package com.net.wanjian.networkhospitalmanager.net;

import android.content.Context;

import com.net.wanjian.networkhospitalmanager.MyApplication;

import java.io.File;
import java.util.concurrent.TimeUnit;

import javax.xml.validation.Validator;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Description:
 * Created by lidongcai on 17/5/3.
 * Email:  lidongcaiocc@163.com
 */

public class OkHttpUtil {
    private volatile static OkHttpClient mOkHttpClient;

    static OkHttpClient getOkHttpUtil(){
        //设置缓存
        File cacheFile = new File(MyApplication.getContext().getCacheDir().getAbsolutePath(),"CurrentCache");
        Cache cache = new Cache(cacheFile, 30*1024*1024);

        if (null == mOkHttpClient){
           synchronized (OkHttpUtil.class){
               if (null == mOkHttpClient){
                   HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
                   interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

                   mOkHttpClient = new OkHttpClient.Builder()
                           .addInterceptor(interceptor)
                           .addNetworkInterceptor(interceptor)
                           .retryOnConnectionFailure(true)
                           //设置请求读写的超时时间,30秒
                           .connectTimeout(30, TimeUnit.SECONDS)
                           .writeTimeout(30, TimeUnit.SECONDS)
                           .readTimeout(30, TimeUnit.SECONDS)
                           .cache(cache)
                           .build();
               }
           }
        }

        return mOkHttpClient;
    }
}
